#include<iostream>
using namespace std;
typedef long long LL;
int main()
{
	LL a, b, p;
	cin >> a >> b >> p;
	LL ans = 1;
	while (b) {
		if (b & 1) {
			ans += (ans * a) % p;
		}
		a = (a + a) % p;
		b >>= 1;
	}
	cout << ans << endl;
	return 0;
}